package edu.gyc.game;

import java.util.*;

class GameHelp {

    public static String userInput(String prompt) {
        System.out.println(prompt);
        String input="";
        Scanner scanner=new Scanner(System.in);
        input=scanner.next();
        return input.toLowerCase();
    }
    /*
    参数 ArrayList<Ship> ships 要放置在棋盘上的3艘船
    本方法是要实现将3艘船随机且不重叠的放置在5*5的棋盘上，也即给每一艘战舰的
    ArrayList<String> locations 属性赋值，注意一定是连续的值，
    比如 美国战舰的locations值为 "11","12","13" ，日本战舰为"22","32","42"
    英国战舰为23","33","43"。
    本方法的难点是随机放置这些战舰，而不越界 5*5的棋盘 横坐标1-5，纵坐标1-5
     */
    //这是简易算法，列举几种可能放置船的位置，随机一个值付给船
    /*
    public static void placeShip(ArrayList<Ship> ships){
        String[] source0={"11","12","13"};
        String[] source1={"14","24","34"};
        String[] source2={"22","32","42"};
        String[] source3={"21","31","41"};
        String[] source4={"23","33","43"};
        ArrayList<String[]> cells=new ArrayList<String[]>();
        String coord="";
        cells.add(source4);
        cells.add(source3);
        cells.add(source2);
        cells.add(source1);
        cells.add(source0);
        for(Ship ship: ships){
            int n=(int )(Math.random()*cells.size());
            String[] s=cells.get(n);
            cells.remove(n);
            String s1="source"+n;
            for(int i=0;i<3;i++){
                ship.getLocations().add(s[i]);
            }
            System.out.println(ship);
        }


    }

     */


    public static void placeShip(List<Ship> ships) {
        //用一个set集合防止坐标重复
        Set<String> zbSet = new HashSet<>();
        Random random = new Random();
        for (int i = 0; i < ships.size(); i++) {
            int direction = random.nextInt(2); //船放置的方向，0横着，1竖着
            int  mid=random.nextInt(3)+2; //船坐标的中值只能是2 ，3 ，4，加1减1才不会越界
            int n=random.nextInt(5)+1;//随机的行或列值，1到5
            System.out.println("direction: "+direction);
            if (direction == 1) {
                //竖着放，row值的中值mid加1减1即可，列就不变
                String top=""+(mid-1)+n;
                String zd=""+(mid-0)+n;
                String bot=""+(mid+1)+n;
                //top,zd,bot若有一个在坐标池zbset中，则该组数据重复，重新寻找
                if (!zbSet.contains(top)&&!zbSet.contains(zd)&&!zbSet.contains(bot)) {
                    zbSet.addAll(Arrays.asList(top, zd, bot));//找到可用坐标组，放入坐标池中
                    ArrayList<String> list = new ArrayList<>();
                    list.addAll(Arrays.asList(top, zd, bot));
                    ships.get(i).setLocations(list);//给当前船放置坐标
                }else {
                    i--;//坐标重复 , 重新运行一次
                }
            }else { //横着放的情况
                String left = "" + n + (mid - 1);
                String zd = "" + n + (mid - 0);
                String right = "" + n + (mid + 1);
                if (!zbSet.contains(left)&&!zbSet.contains(zd)&&!zbSet.contains(right)) {
                    zbSet.addAll(Arrays.asList(left, zd, right));
                    ArrayList<String> list = new ArrayList<>();
                    list.addAll(Arrays.asList(left, zd, right));
                    ships.get(i).setLocations(list);
                }else {
                    i--;
                }
            }
            System.out.println("i: "+i);
            System.out.println("zbset: "+zbSet);
        }
        System.out.println(ships);
    }

}